package com.zjx.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.reggie.common.R;
import com.zjx.reggie.domain.*;
import com.zjx.reggie.service.AddressBookService;
import com.zjx.reggie.service.OrdersService;
import com.zjx.reggie.service.ShoppingCartService;
import com.zjx.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session) {
        log.info(orders.toString());
        ordersService.saveOrdersWithOrderDetail(orders);

//        AtomicInteger amount = new AtomicInteger(0);
//        long orderId = IdWorker.getId();//订单号
//        Long userId = (Long) session.getAttribute("user");
//        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
//        orders.setAddress(
//                addressBook.getDistrictName()==null? " ": addressBook.getDistrictName()
//                        +addressBook.getCityName()==null? " ":addressBook.getCityName()
//                        +addressBook.getCityCode()==null?" ":addressBook.getCityCode()
//                        +addressBook.getDetail()==null?" ":addressBook.getDetail()
//        );
//        //查询user，姓名
//        User user = userService.getById(userId);
//
//        orders.setId(orderId);
//        orders.setOrderTime(LocalDateTime.now());
//        orders.setCheckoutTime(LocalDateTime.now());
//        orders.setStatus(2);
//        orders.setAmount(new BigDecimal(amount.get()));//总金额
//        orders.setUserId(userId);
//        orders.setNumber(String.valueOf(orderId));
//        orders.setUserName(user.getName());
//        orders.setConsignee(addressBook.getConsignee());
//        orders.setPhone(addressBook.getPhone());
//
//        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper =new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
//        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
//        list.stream().map((item)->{
//            OrderDetail orderDetail = new OrderDetail();
//            orderDetail.setOrderId(orderId);
//            orderDetail.setNumber(item.getNumber());
//            orderDetail.setDishFlavor(item.getDishFlavor());
//            orderDetail.setDishId(item.getDishId());
//            orderDetail.setSetmealId(item.getSetmealId());
//            orderDetail.setName(item.getName());
//            orderDetail.setImage(item.getImage());
//            orderDetail.setAmount(item.getAmount());
//            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
//            return orderDetail;
//
//        })

        return R.success("下单成功");
    }

    //订单查询功能
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String beginTime, String endTime) {

        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        ordersService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }


}
